package cn.yudao.order.service.impl;

import cn.yudao.basic.exception.BusinessException;
import cn.yudao.order.domain.OrderPetAcquisition;
import cn.yudao.order.domain.OrderPetAcquisitionLog;
import cn.yudao.order.dto.AccomplishDto;
import cn.yudao.order.mapper.OrderPetAcquisitionLogMapper;
import cn.yudao.order.mapper.OrderPetAcquisitionMapper;
import cn.yudao.order.service.IOrderPetAcquisitionService;
import cn.yudao.basic.service.impl.BaseServiceImpl;
import cn.yudao.pet.domain.Pet;
import cn.yudao.pet.domain.SearchMasterMsg;
import cn.yudao.pet.mapper.PetMapper;
import cn.yudao.pet.mapper.SearchMasterMsgMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yudao
 * @since 2022-07-19
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private OrderPetAcquisitionLogMapper orderPetAcquisitionLogMapper;
    /**
    * @Title: cancel
    * @Description: 店员接单后取消订单
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/7/20 18:25
    * @Parameters:
    * @Return
    */
    @Override
    public void cancel(Long id) {
        // 根据id查询到当前订单表对应的数据
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.loadById(id);
        // 设置订单状态为null
        orderPetAcquisition.setState(-1);
        orderPetAcquisition.setPetId(null);
        orderPetAcquisition.setUserId(null);
        orderPetAcquisitionMapper.upadte(orderPetAcquisition);
        // 我们先根据订单中的msgid查询到对应的寻主信息msg
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.loadById(orderPetAcquisition.getSearchMasterMsgId());
        // 删除寻主信息的shopid -- 放入寻主池中
        searchMasterMsg.setShopId(null);
        searchMasterMsgMapper.upadte(searchMasterMsg);
        // 删除当前店铺的pet中相关的宠物信息
        petMapper.removeById(orderPetAcquisition.getPetId());
        // 新增一个订单失败的日志表
        extracted(0,orderPetAcquisition);
    }

    /**
    * @Title: extracted
    * @Description: 成功或者失败日志表
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/7/20 20:20
    * @Parameters:
    * @Return
    */
    private void extracted(Integer state,OrderPetAcquisition orderPetAcquisition) {
        Long id = orderPetAcquisition.getId();
        OrderPetAcquisitionLog orderPetAcquisitionLog = new OrderPetAcquisitionLog();
        orderPetAcquisitionLog.setAcqId(id);
        orderPetAcquisitionLog.setAuditTime(new Date());
        orderPetAcquisitionLog.setState(state);
        if (state == 0){
            orderPetAcquisitionLog.setNote("订单失败");
        }else if (state == 1){
            orderPetAcquisitionLog.setNote("完成订单");
        }
        orderPetAcquisitionLogMapper.save(orderPetAcquisitionLog);
    }

    /**
    * @Title: confirm
    * @Description: 确认订单完成的订单
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/7/20 19:51
    * @Parameters:
    * @Return
    */
    @Override
    public void confirm(AccomplishDto accomplishDto) {
        // 非空判断
        Long id = accomplishDto.getId();
        BigDecimal money = accomplishDto.getMoney();
        Integer payType = accomplishDto.getPayType();
        if (id == null || money == null || payType == null){
            throw new BusinessException("请正确填写参数");
        }
        // 获取当前的订单
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.loadById(id);
        // 修改当前订单的状态
        orderPetAcquisition.setState(1);
        // 修改订单的基本信息比如进价
        orderPetAcquisition.setPrice(money);
        // 设置状态为垫付
        orderPetAcquisition.setPaytype(payType);
        orderPetAcquisitionMapper.upadte(orderPetAcquisition);
        // 修改宠物寻主信息
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.loadById(orderPetAcquisition.getSearchMasterMsgId());
        searchMasterMsg.setState(2); // 状态为2
        searchMasterMsgMapper.upadte(searchMasterMsg);
        // 修改宠物表的信息
        Pet pet = petMapper.loadById(orderPetAcquisition.getPetId());
        pet.setState(0);
        pet.setCostprice(money);
        petMapper.upadte(pet);
        // 新增一个订单成功的日志表
        extracted(1,orderPetAcquisition);
    }
}
